<?php

/**
 * main actions.
 *
 * @package    hybrid
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class mainActions extends sfActions
{
	/**
	* Executes index action
	*
	* @param sfRequest $request A request object
	*/
	public function executeIndex(sfWebRequest $objRequest)
	{
		$this->getUser()->signOut();
		if ($objRequest->getParameter('mailaddress') != '') {
			$this->strMailAddress = $objRequest->getParameter('mailaddress');
		}
		if ($objRequest->getParameter('password') != '') {
			$this->strPassWord = $objRequest->getParameter('password');
		}
		return sfView::SUCCESS;
	}

	/**
	* Executes login action
	*
	* @param sfRequest $request A request object
	*/
	public function executeLogin(sfWebRequest $objRequest)
	{
		$objCriteria = new Criteria();
		$objCriteria->add( UserTablePeer::IS_DELETED, FALSE );
		$objCriteria->add( UserTablePeer::MAIL_ADDRESS, $this->getRequestParameter("mailaddress") );
		$strMailAddress = $objRequest->getParameter('mailaddress');
		$strPassWord = $objRequest->getParameter('password');
		if ($strMailAddress == '') {
			$objRequest->setParameter("error","Hãy Nhập Email Của Bạn !");
			$this->forward("main","index");
		}
		if ($strPassWord == '') {
			$objRequest->setParameter("error","Hãy Nhập Password Của Bạn !");
			$this->forward("main","index");
		}
		if ( UserTablePeer::doCount( $objCriteria ) ) {
			$objCriteria->add( UserTablePeer::PASSWORD_MD5, md5($this->getRequestParameter("password")) );
			if ( UserTablePeer::doCount( $objCriteria ) ) {
				$objTemp = UserTablePeer::doSelectOne( $objCriteria );
				$this->getUser()->Initialization( $objTemp->getId() );

//				if ($request->getParameter("rememberlogin")) {
//					$objTemp->setIsAutoLogin(TRUE);
//				} else {
//					$objTemp->setIsAutoLogin(FALSE);
//				}
				$objTemp->save();
			} else {
				$objRequest->setParameter("error","PassWord Sai !");
				$this->forward("main","index");
			}
		} else {
			$objRequest->setParameter("error","User Không Tồn Tại !");
			$this->forward("main","index");
		}

		$this->redirect("main/home");
		
	}
	
	/**
	 * Check validation
	 *
	 * @return boolean
	 */
	public function validateLogin()
	{
		$objRequest = $this->getRequest();
	
		if ($objRequest->hasErrors()) {
			return false;
		}

		return true;
	}
	
	/**
	* display error message when edit action has error
	* @return sfView
	*/
	public function handleErrorLogin()
	{
		$objRequest = $this->getRequest();
		$this->strMailAddress = $objRequest->getParameter('mailaddress');
		$this->strPassWord = $objRequest->getParameter('password');

		return sfView::SUCCESS;
	}
	/**
	* Executes index action
	*
	* @param sfRequest $request A request object
	*/
	public function executeHome(sfWebRequest $request)
	{
		return sfView::SUCCESS;
	}
	
	/**
	* Executes logout action
	*
	* @param sfRequest $request A request object
	*/
	public function executeLogout(sfWebRequest $request)
	{
		$this->getUser()->signOut();

		$this->redirect("main/index");
	}
	
}
